package com.huaxonline.boot.web.pojo.response;

import com.huaxonline.boot.web.constant.IResponseEnum;
import com.huaxonline.boot.web.constant.enums.CommonResponseEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.ToString;

/**
 * 响应信息主体，同{@link QueryDataResponse}
 *
 * @param <T>
 * @author sprainkle
 * @date 2019/5/2
 * @see QueryDataResponse
 */
@ToString
@Schema(description = "响应主体(含分页数据集)")
public class QR<T> extends QueryDataResponse<T> {

    public QR() {
        super();
    }

    public QR(PageResult<T> data) {
        super(data);
    }

    public static <T> QR<T> ok() {
        return restResult(CommonResponseEnum.SUCCESS);
    }

    public static <T> QR<T> ok(String msg) {
        return restResult(CommonResponseEnum.SUCCESS.getCode(), msg);
    }

    public static <T> QR<T> ok(PageResult<T> data) {
        return restResult(CommonResponseEnum.SUCCESS, data);
    }

    public static <T> QR<T> ok(String msg, PageResult<T> data) {
        return restResult(CommonResponseEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> QR<T> failed() {
        return restResult(CommonResponseEnum.FAIL);
    }

    public static <T> QR<T> failed(String msg) {
        return restResult(CommonResponseEnum.FAIL.getCode(), msg);
    }

    public static <T> QR<T> failed(PageResult<T> data) {
        return restResult(CommonResponseEnum.FAIL, data);
    }

    public static <T> QR<T> failed(String msg, PageResult<T> data) {
        return restResult(CommonResponseEnum.FAIL.getCode(), msg, data);
    }

    public static <T> QR<T> failed(IResponseEnum iResponseEnum) {
        return restResult(iResponseEnum);
    }

    public static <T> QR<T> failed(IResponseEnum iResponseEnum, PageResult<T> data) {
        return restResult(iResponseEnum, data);
    }

    public static <T> QR<T> restResult(IResponseEnum iResponseEnum) {
        return restResult(iResponseEnum.getCode(), iResponseEnum.getMessage());
    }

    public static <T> QR<T> restResult(IResponseEnum iResponseEnum, PageResult<T> data) {
        return restResult(iResponseEnum.getCode(), iResponseEnum.getMessage(), data);
    }

    private static <T> QR<T> restResult(int code, String msg) {
        QR<T> apiResult = new QR<>();
        apiResult.setCode(code);
        apiResult.setMessage(msg);
        return apiResult;
    }

    private static <T> QR<T> restResult(int code, String msg, PageResult<T> data) {
        QR<T> apiResult = new QR<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }
}
